<?php
Globals::requireController('Admin');
Globals::requireTable('Vipuser');
Globals::requireTable('Needslog');
Globals::requireTable('Product');
Globals::requireTable('Msg');
Globals::requireTable('Vmoney');
Globals::requireTable('Vlevel');

class VipsController extends AdminController
{
    protected $vipuserTable;
    protected $needslogTable;
    protected $productTable;
    protected $msgTable;
    protected $vmoneyTable;
    protected $vlevelTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->vipuserTable = new VipuserTable($config);
		$this->needslogTable = new NeedslogTable($config);
		$this->productTable = new ProductTable($config);
		$this->msgTable = new MsgTable($config);
		$this->vmoneyTable = new VmoneyTable($config);
		$this->vlevelTable = new VlevelTable($config);
	}
	
	public function indexAction()
	{
	    $where = array();
	    
	    $params = array();
	    
	    $userid = $this->getIntParam('userid');
	    if($userid > 0){
	        $where['id'] = $userid;
	        $params['userid'] = $userid;
	    }
	    
	    
	    $this->view->params = $params;
	    
	    $count = $this->vipuserTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->vipuserTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    
	    foreach ($data as $k=>$v){
	        $sql = "select SUM(level) as su from tb_vlevel where uid = {$v['id']}";
    	    $re = $this->vipuserTable->dosql($sql);
    	    $data[$k]['level_count'] = empty($re['su']) ? 0 : $re['su'];
    	    
    	    $sql = "select SUM(money) as su from tb_vmoney where uid = {$v['id']}";
    	    $re = $this->vipuserTable->dosql($sql);
    	    $data[$k]['money_count'] = empty($re['su']) ? 0 : $re['su'];
    	    
    	    $sql = "select SUM(money) as su from tb_order where uid_from = '{$v['id']}' and status > 0 and status <> 4 and type = 1";
    	    $re = $this->vipuserTable->dosql($sql);
    	    $data[$k]['income_count'] = empty($re['su']) ? 0 : $re['su'];
	    }
	    //var_dump($data);
	    $this->view->data = $data;
	    
	}
	
	public function needslistAction(){
	    $uid = $this->getIntParam('uid');
	    $where = array('uid'=>$uid);
	    $count = $this->needslogTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->needslogTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    foreach ($data as $k=>$v){
	        $data[$k]['product'] = $this->productTable->getRow($v['pid']);
	    }
	    $this->view->data = $data;
	}
	
	public function msglistAction(){
	    $uid = $this->getIntParam('uid');
	    $where = array('uid'=>$uid);
	    $this->view->uid = $uid;
	    $count = $this->msgTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->msgTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    $this->view->data = $data;
	    
	    global $msg_status;
	    $this->view->msg_status = $msg_status;
	}
	
	public function sendmsgAction(){
	    $status = $this->getIntParam('status');
	    switch ($status){
	        case 1:
	            $msg = '发送成功';
	            break;
	        default:
	            $msg = '';
	            break;
	    }
	    $this->view->msg = $msg;
	}
	
	public function dosendmsgAction(){
	    set_time_limit(0);
	    $where = array();
	    $uid = $this->getIntParam('uid');
	    if($uid > 0){
	        $where['id'] = $uid;
	    }
	    $date = date('Y-m-d H:i:s');
	    $msg = $this->getParam('content');
	    $users = $this->vipuserTable->listAllWithFields('id',$where);
	    foreach ($users as $v){
	        $tmp_data['uid'] = $v['id'];
	        $tmp_data['createdate'] = $date;
	        $tmp_data['msg'] = $msg;
	        $tmp_data['status'] = 0;
	        $this->msgTable->add($tmp_data);
	    }
	    if($uid > 0){
	        $this->redirectToAction('msglist',array('uid'=>$uid));
	    }else{
	        $this->redirectToAction('sendmsg',array('status'=>1));
	    }
	    exit();
	}
	
	public function vmoneyAction(){
	    $uid = $this->getIntParam('uid');
	    if($uid < 1){
	        $this->redirect('/');
	        exit();
	    }
	    $user = $this->vipuserTable->getRow($uid);
	    $this->view->user = $user;
	    
	    $where = array('uid'=>$uid);
	    $count = $this->vmoneyTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->vmoneyTable->listPage($where, 'id DESC', $pageSize, $pageId);
	}
	
	public function vmoneydetailAction(){
	    $uid = $this->getIntParam('uid');
	    $this->view->uid = $uid;
	    if($uid < 1){
	        $this->redirect('/');
	        exit();
	    }
	}
	
	public function vmoneysubmitAction(){
	    $data['uid'] = $this->getIntParam('uid');
	    if($data['uid'] < 1){
	        $this->redirect('/');
	        exit();
	    }
	    $data['money'] = $this->getParam('money');
	    $data['remark'] = $this->getParam('remark');
	    $data['type'] = 2;
	    $this->vmoneyTable->add($data);
	    $this->redirect('./?controller=vips&action=vmoney&uid='.$data['uid']);
	    exit();
	}
	
	public function vlevelAction(){
	    $uid = $this->getIntParam('uid');
	    if($uid < 1){
	        $this->redirect('/');
	        exit();
	    }
	    $user = $this->vipuserTable->getRow($uid);
	    $this->view->user = $user;
	     
	    $where = array('uid'=>$uid);
	    $count = $this->vlevelTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->vlevelTable->listPage($where, 'id DESC', $pageSize, $pageId);
	}
	
	public function vleveldetailAction(){
	    $uid = $this->getIntParam('uid');
	    $this->view->uid = $uid;
	    if($uid < 1){
	        $this->redirect('/');
	        exit();
	    }
	}
	
	public function vlevelsubmitAction(){
	    $data['uid'] = $this->getIntParam('uid');
	    if($data['uid'] < 1){
	        $this->redirect('/');
	        exit();
	    }
	    $data['level'] = $this->getParam('level');
	    $data['remark'] = $this->getParam('remark');
	    $data['type'] = 2;
	    $this->vlevelTable->add($data);
	    $this->redirect('./?controller=vips&action=vlevel&uid='.$data['uid']);
	    exit();
	}
	
}
